<?php
namespace app\yunda\model;

use think\Model;
use traits\model\SoftDelete;

/**
 * 用户表
 * @package app\yunda\model
 */
class User extends Model
{
    use SoftDelete;
    protected $table = 'dp_yd_user';
    protected $autoWriteTimestamp = true;

    public function setPasswordAttr($value)
    {
        return $value=md5($value);
    }

    //获取员工
    public static function getYuanGong($openid)
    {
        $list=self::where(['user_type'=>1,'openid'=>['notin',[$openid]]])->column('id,nickname');
        if(empty($list)) return $list[0]='暂无员工,请去添加员工';
        return $list;
    }

    //关联订单表，一对多,快递员
    public function Orders()
    {
        return $this->hasMany('Order','pai_id');
    }

    //获取某个用户信息
    public function  getUserInfo($id)
    {
        $user=self::get($id);
        if(empty($user))return '用户不存在';
        return $user;
    }

    //查询当前用户绑定的上三个级别，
    public static function getFenXiao($bind_id)
    {
        $arr=[];
        $one=self::get($bind_id);
        if(!empty($one)){
            $arr['one_leader']=$one['id'];
            $two=self::get($one['id']);
            if(!empty($two)) {
                $arr['two_leader']=$two['id'];
                $three=self::get($two['id']);
                if(!empty($three))$arr['two_leader']=$three['id'];
            }
        }
        return $arr;
    }
}
